<script type='text/javascript' src='./lib/zip.js'></script>
<script>

zip.workerScriptsPath = "./lib/";

function DP(in_data)
{
	return;
	if (!console) {
		return;
	}
	var _inspect = function(in_obj, in_indent) {
		for (var prop in in_obj) {
			var child = false;
			if ((typeof(in_obj[prop]) == 'object') && (in_obj[prop])) {
				child = true;
			}
			console.log(in_indent + prop + ' : ' + in_obj[prop]);
			if (child) {
				_inspect(in_obj[prop], in_indent + '  ');
			}
		}
	};
	_inspect({DP : in_data}, '');
}

function BIND(callback, self)
{
	return function() {
		callback.apply(self, arguments);
	}
}

var zipMgr = {
	reader : null,
	reading : 0,
	checker : null,
	callback : null,
	done : null,
	handleErr : function(in_err) {
		DP(in_err);
	},
	handleProgress : function(in_filename) {
		return function(in_cur, in_total) {
			DP(in_filename + ' : ' + in_cur + '(' + in_total + ')');
		};
	},
	handleClose : function() {
		DP('closed');
	},
	handleDone : function(in_filename) {
		return function(in_text) {
			(this.callback)(in_filename, in_text);
			var handleClose = BIND(this.handleClose, this);
			if (--this.reading > 0) {
				return;
			}
			this.reader.close(handleClose);
			(this.done)();
			this.reader = null;
			this.checker = null;
			this.callback = null;
			this.done = null;
		};
	},
	handleStart : function(in_reader) {
		this.reader = in_reader;
		var handleEntries = BIND(this.handleEntries, this);
		this.reader.getEntries(handleEntries);
	},
	handleEntries : function(in_entries) {
		if (in_entries.length > 0) {
			for (var i = 0; i < in_entries.length; i++) {
				var filename = in_entries[i].filename;
				if ((this.checker)(filename)) {
					this.reading++;
				} else {
					continue;
				}
				var handleDone = BIND(this.handleDone(filename), this);
				var handleProgress = BIND(this.handleProgress(filename), this);
				in_entries[i].getData(new zip.TextWriter(), handleDone, handleProgress);
			}
		}
	},
	parse : function(in_data, in_callback, in_checker, in_done) {
		this.callback = in_callback;
		this.checker = in_checker;
		this.done = in_done;
		var handleStart = BIND(this.handleStart, this);
		var handleErr = BIND(this.handleErr, this);
		zip.createReader(new zip.Data64URIReader(in_data), handleStart, handleErr);
	}
};

function _1st_value(parent, ename)
{
	var elems = parent.getElementsByTagName(ename);
	if (elems.length > 0) {
		return elems.item(0).firstChild.nodeValue;
	} else {
		return null;
	}
}

function _r2rc(in_r)
{
	if (!in_r.match(/^([A-Z]+)([0-9]+)$/)) {
		return null;
	}
	var col = RegExp.$1;
	var v = 0;
	for (var i = 0; i < col.length; i++) {
		v += (col.charCodeAt(i) - 64) * Math.pow(26, col.length - i - 1);
	}
	return {row : (RegExp.$2 - 1), col : (v - 1)};
}

var xlsxObj = {
	docs : {},
	getTable : function(in_sheet) {
		var sheet = in_sheet;
		if (!sheet) {
			var sheet = 'sheet1.xml'
		}
		var sharedStrings = (this.docs['xl/sharedStrings.xml']).getElementsByTagName('si');
		var sheetData = (this.docs['xl/worksheets/' + sheet]).getElementsByTagName('sheetData').item(0);
		/*
			<sheetData>
				<row>
					<c r='...' t='...'><v>...</v></c>
					...
				</row>
				...
			</sheetData>
		*/
		var max = {
			row : 0,
			col : 0
		};
		var nodes = {};
		var rows = sheetData.getElementsByTagName('row');
		for (var i = 0; i < rows.length; i++) {
			var cells = rows.item(i).getElementsByTagName('c');
			for (var j = 0; j < cells.length; j++) {
				var c = cells.item(j);
				var r = _r2rc(c.getAttribute('r'));
				var v = _1st_value(c, 'v');
				if (c.getAttribute('t') == 's') {
					v = _1st_value(sharedStrings.item(v), 't');
				}
				/* style should be set */
				nodes[r.row + '_' + r.col] = v;
				if (max.row < r.row) {
					max.row = r.row;
				}
				if (max.col < r.col) {
					max.col = r.col;
				}
			}
		}
		var ret = [];
		for (var row = 0; row < max.row + 1; row++) {
			ret[row] = [];
			for (var col = 0; col < max.col + 1; col++) {
				if (typeof(nodes[row + '_' + col]) == 'undefined') {
					ret[row][col] = null;
				} else {
					ret[row][col] = nodes[row + '_' + col];
				}
			}
		}
		return ret;
	}
};

function main(in_file)
{
	with (new FileReader()) {
		onload = function(e) {
			zipMgr.parse(e.target.result,
				function(in_filename, in_xml) {
					xlsxObj.docs[in_filename] = (new DOMParser()).parseFromString(in_xml, 'application/xml');
				},
				function(in_path) {
					if (in_path.match(/\.xml$/)) {
						return true;
					} else {
						return false;
					}
				},
				function() {
					var tbl = document.getElementById('tbl');
					var dat = xlsxObj.getTable();
					for (var row = 0; row < dat.length; row++) {
						var tr = document.createElement('TR');
						for (var col = 0; col < dat[row].length; col++) {
							var td = document.createElement('TD');
							if (dat[row][col] != null) {
								td.appendChild(document.createTextNode(dat[row][col]));
							}
							tr.appendChild(td);
						}
						tbl.appendChild(tr);
					}
				}
			);
		}
		readAsDataURL(in_file);
	}
}

window.addEventListener('dragover', function(e) {
	e.preventDefault();
}, false);

window.addEventListener('drop', function(e) {
	main(e.dataTransfer.files[0]);
	e.preventDefault();
	e.stopPropagation();
}, false);

</script>
<style type='text/css'>
TABLE {
	border-collapse: collapse;
}
TABLE, TD {
	border: solid 1px black;
	padding: 3px;
}
</style>
<table id='tbl'>
</table>
